package learn.emp02.ex02;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> idCards = new ArrayList<>();
        List<String> dates = new ArrayList<>();
        int n = scanner.nextInt();
        scanner.nextLine();
        for (int i = 0; i < n; i++) {
            String idCard = scanner.nextLine();
            idCards.add(idCard);
            String year = idCard.substring(6, 10);
            String month = idCard.substring(10, 12);
            String day = idCard.substring(12, 14);
            dates.add(year + "-" + month + "-" + day);
        }
        while (true) {
            String command = scanner.nextLine();
            if ("sort1".equals(command)) {
                dates.sort(String::compareTo);
                for (String date : dates) {
                    System.out.println(date);
                }
            } else if ("sort2".equals(command)) {
                idCards.sort((s1, s2) -> {
                    String date1 = s1.substring(6, 14);
                    String date2 = s2.substring(6, 14);
                    return date1.compareTo(date2);
                });
                for (String idCard : idCards) {
                    System.out.println(idCard);
                }
            } else if ("e".equals(command)) {
                System.out.println("exit");
                break;
            } else {
                System.out.println("exit");
                break;
            }
        }
        scanner.close();
    }
}
